package com.jtf.esdemo.controller;

import com.jtf.esdemo.domain.entity.TestEntity;
import com.jtf.esdemo.repository.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class TestController {

    @Autowired
    private TestRepository testRepository;

    @GetMapping(value = "/test")
    public String save(TestEntity testEntity){
        testRepository.save(testEntity);
        return "成功";
    }

    @GetMapping(value = "/findAll")
    public  Page<TestEntity> getAll(Integer pageNo, Integer pageSize){
        List<TestEntity> list = new ArrayList<>();
        Page<TestEntity> all = testRepository.findAll(PageRequest.of(pageNo, pageSize));
        return all;
    }

    @GetMapping(value = "/start")
    public void start(Integer start){

        Random random = new Random();
        for (int i = start; i < 1000000; i++) {
            String ip = random.nextInt(255)+"."+random.nextInt(255)+"."+random.nextInt(255)+"."+random.nextInt(255);
            TestEntity testEntity = new TestEntity();
            testEntity.setId(Long.valueOf(i));
            testEntity.setIp(ip);
            testEntity.setType(random.nextInt(10));
            testEntity.setVisitTime(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
            );
            testRepository.save(testEntity);
            System.out.println(i);
        }


    }
}
